package org.peipito.prs;

import org.peipito.prs.engine.GameEngine;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class MainGamePanel extends SurfaceView implements Callback {

	private MainThread thread;

	private static final String TAG = MainGamePanel.class.getSimpleName();
	
	RPSWhite rpsWhite;
	RPSBlack rpsBlack;
	Result result;
	CountDown countDown;
	
	/**
	 * Engine will be initialized by the setter.
	 */
	private GameEngine gameEngine; 
	
	public MainGamePanel(Context context) {
		super(context);
		
		getHolder().addCallback(this);
		
		Bitmap [] countDowns = { 
				BitmapFactory.decodeResource(getResources(), R.drawable.zero),
				BitmapFactory.decodeResource(getResources(), R.drawable.un),
				BitmapFactory.decodeResource(getResources(), R.drawable.deux),
				BitmapFactory.decodeResource(getResources(), R.drawable.trois),
				BitmapFactory.decodeResource(getResources(), R.drawable.quatre),
				BitmapFactory.decodeResource(getResources(), R.drawable.cinq)
		};
		
		Bitmap [] blackPRS = {
				BitmapFactory.decodeResource(getResources(), R.drawable.rockblack),
				BitmapFactory.decodeResource(getResources(), R.drawable.paperblack),
				BitmapFactory.decodeResource(getResources(), R.drawable.scissorblack)
		};
		
		Bitmap [] bitmapResult = {
				BitmapFactory.decodeResource(getResources(), R.drawable.winner),
				BitmapFactory.decodeResource(getResources(), R.drawable.draw),
				BitmapFactory.decodeResource(getResources(), R.drawable.looser)
		};
		
		Bitmap [] bitmapResultClicked = {
				BitmapFactory.decodeResource(getResources(), R.drawable.winnerclicked),
				BitmapFactory.decodeResource(getResources(), R.drawable.drawclicked),
				BitmapFactory.decodeResource(getResources(), R.drawable.looserclicked)
		};
		
		rpsWhite = new RPSWhite(
				BitmapFactory.decodeResource(getResources(), R.drawable.rockwhite),
				BitmapFactory.decodeResource(getResources(), R.drawable.paperwhite),
				BitmapFactory.decodeResource(getResources(), R.drawable.cissorswhite),
//				BitmapFactory.decodeResource(getResources(), R.drawable.back_screen),
				BitmapFactory.decodeResource(getResources(), R.drawable.rpswhitemenudow),
//				countDowns,
				0, 0);
		
		rpsBlack = new RPSBlack(blackPRS,this);
		result 	 = new Result(bitmapResult,bitmapResultClicked); 
		
		countDown = new CountDown(countDowns);
		
		// now the game loop thread is created in the surfaceCreated method.
		// create the game loop thread
		 thread = new MainThread(getHolder(), this);
		
		// make the GamePanel focusable so it can handle events
		setFocusable(true);
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		
	}
	
	public void surfaceCreated(SurfaceHolder holder) {
		thread = new MainThread(getHolder(), this);
		thread.setRunning(true);
		thread.start();
	}
	
	public void surfaceDestroyed(SurfaceHolder holder) {
		Log.d(TAG, "Surface is being destroyed");
		boolean retry = true;
		while (retry) {
			try {
				rpsWhite.init();
				thread.setRunning(false);
				thread.join(1000);
				retry = false;
			} catch (InterruptedException e) {
				// try again shutting down the thread
			}
		}
		Log.d(TAG, "Thread was shut down cleanly");
	}
	
	@Override
	public boolean onTouchEvent(android.view.MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_DOWN){
			rpsWhite.handleActionDown((int) event.getX(),(int) event.getY(),this);
			result.handleActionDown((int) event.getX(),(int) event.getY(),this);
		}
		return true;
	};
	
	public void render(Canvas canvas){
//		Log.d(TAG,"render");
		canvas.drawColor(Color.BLACK);
		rpsWhite.draw(canvas,this);
		rpsBlack.draw(canvas,this);
		result.draw(canvas, this);
		countDown.draw(canvas,this);
	}
	
	/**
	 * This is the game update method. It iterates through all the objects
	 * and calls their update method if they have one or calls specific
	 * engine's update method.
	 */
	public void update(){
		long time = System.currentTimeMillis();
		rpsWhite.update(time);
		rpsBlack.update(time);
		result.update(time);
		countDown.update(time);
	}
	
	public void init(){
		gameEngine.init();
		rpsWhite.init();
		rpsBlack.init();
		result.init();
		countDown.init();
	}
	
	public void setEngine(GameEngine gameEngine){
		this.gameEngine = gameEngine;
		rpsBlack.setEngine(gameEngine);
		rpsWhite.setEngine(gameEngine);
		result.setEngine(gameEngine);
		countDown.setEngine(gameEngine);
	}
	
}
